package com.whlianta.tax;

import android.os.Bundle;
import android.os.Environment;
import android.os.PersistableBundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bytedance.ads.convert.BDConvert;
import com.umeng.commonsdk.UMConfigure;

import com.yl.adsdk.YlLib;

import java.io.File;

import io.flutter.embedding.android.FlutterFragmentActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterFragmentActivity {

    private static final String channelName = "ts_flutter_plugin";
    private static final String spName = "my_preferences_flutter";
    private static final String appChannel = BuildConfig.app_channel;
    private static final String urlUserAgreement = BuildConfig.URL_USER_AGREEMENT;
    private static final String urlPrivacyPolicy = BuildConfig.URL_PRIVACY_POLICY;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        boolean privacyAccept = getSharedPreferences(spName, MODE_PRIVATE)
                .getBoolean("privacy_accept1", false);
        if (privacyAccept) {
            UMConfigure.setLogEnabled(false);
            UMConfigure.preInit(this, "66da645250a80a550eda9129", appChannel);
        }

    }

    public String getInternalExternalStorageRoot() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File externalStorageDir = Environment.getExternalStorageDirectory();
            return externalStorageDir != null ? externalStorageDir.getAbsolutePath() : null;
        }
        return null;
    }

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
        MethodChannel methodChannel = new MethodChannel(flutterEngine.getDartExecutor(), channelName);
        methodChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
                switch (call.method) {
                    case "getChannel":
                        result.success(appChannel);
                        break;
                    case "getAppName":
                        result.success(getResources().getString(R.string.my_app_name));
                        break;
                    case "getServiceUrl":
                        result.success(urlUserAgreement);
                        break;
                    case "getPolicyUrl":
                        result.success(urlPrivacyPolicy);
                        break;
                    case "desDecrypt":
                        String strKey = call.argument("strKey");
                        String content = call.argument("content");
                        DESPlus desPlus = null;
                        try {
                            desPlus = new DESPlus(strKey);
                            result.success(desPlus.decrypt(content));
                        } catch (Exception e) {
                            result.success("");
                        }
                        break;
                    case "getSimOperator":
                        int simOperator = DeviceInfoUtil.getSimOperator(MainActivity.this);
                        result.success(simOperator);
                        break;
                    case "getWebViewUserAgent":
                        String webViewUserAgent = DeviceInfoUtil.getWebViewUserAgent(MainActivity.this);
                        result.success(webViewUserAgent);
                        break;
                    case "getAppUserAgent":
                        String appUserAgent = DeviceInfoUtil.getAppUserAgent();
                        result.success(appUserAgent);
                        break;
                    case "getImei":
                        String imei = DeviceInfoUtil.getImei(MainActivity.this);
                        result.success(imei);
                        break;
                    case "getPhoneMEID":
                        String meid = DeviceInfoUtil.getPhoneMEID(MainActivity.this);
                        result.success(meid);
                        break;
                    case "getDeviceId":
                        String deviceId = DeviceInfoUtil.getDeviceId(MainActivity.this);
                        result.success(deviceId);
                        break;
                    case "getAndroidId":
                        String androidId = DeviceInfoUtil.getAndroidId(MainActivity.this);
                        result.success(androidId);
                        break;
                    case "getOaId":
                        DeviceInfoUtil.getOaid(MainActivity.this, new DeviceInfoUtil.OnCompleteListener() {
                            @Override
                            public void onComplete(String data) {
                                result.success(data);
                            }
                        });
                        break;
                    case "getOaidU":
                        DeviceInfoUtil.getOaidU(MainActivity.this, new DeviceInfoUtil.OnCompleteListener() {
                            @Override
                            public void onComplete(String data) {
                                result.success(data);
                            }
                        });
                        break;
                    case "getOaidH":
                        DeviceInfoUtil.getOaidH(MainActivity.this, new DeviceInfoUtil.OnCompleteListener() {
                            @Override
                            public void onComplete(String data) {
                                result.success(data);
                            }
                        });
                        break;
                    case "getLocalIpV6":
                    case "getMacFromHardware":
                    case "getIPAddress":
                        result.success("");
                        break;
                    case "getSystemModel":
                        String systemModel = DeviceUtils.getSystemModel();
                        result.success(systemModel);
                        break;
                    case "getSystemCode":
                        int systemCode = DeviceUtils.isHarmonyOs() ? 2 : 0;
                        result.success(systemCode);
                        break;
                    case "getSystemVersion":
                        String systemVersion = DeviceUtils.getSystemVersion();
                        result.success(systemVersion);
                        break;
                    case "getHarmonyVersion":
                        String harmonyVersion = DeviceUtils.getHarmonyVersion();
                        result.success(harmonyVersion);
                        break;
                    case "isBatteryCharging":
                        boolean isCharging = BatteryUtils.isBatteryCharging(MainActivity.this);
                        result.success(isCharging);
                        break;
                    case "getNetworkType":
                        String networkType = NetworkUtils.getNetworkType(MainActivity.this);
                        result.success(networkType);
                        break;
                    case "getBrand":
                        String brand = DeviceUtils.getBrand();
                        result.success(brand);
                        break;
                    case "getRiskInfo":
                        String riskInfo = YlLib.getRiskInfo(MainActivity.this);
                        result.success(riskInfo);
                        break;
                    case "showRewardTip":
                        YlLib.showRewardTip();
                        break;
                    case "setCheckPrivacy":
                        YlLib.setCheckPrivacy();
                        break;
                    case "BDConvertInit":
                        BDConvert.INSTANCE.init(MainActivity.this,MainActivity.this);
                    case "privacyAcceptFalse":
                        getSharedPreferences(spName, MODE_PRIVATE).edit()
                                .putBoolean("privacy_accept1", false).apply();
                        break;
                    case "privacyAcceptTrue":
                        getSharedPreferences(spName, MODE_PRIVATE).edit()
                                .putBoolean("privacy_accept1", true).apply();
                        break;
                    case "isNormalUser":
                        boolean isNormal = DeviceUtils.hasSimCard(MainActivity.this)
                                && !DeviceUtils.isRoot()
                                && !DeviceUtils.usbStatus(MainActivity.this)
                                && !EmulatorUtils.isEmulator2(MainActivity.this)
                                && !BatteryUtils.isBatteryCharging(MainActivity.this);
                        result.success(isNormal);
                        break;
                    default:
                        result.notImplemented();
                }
            }
        });
    }

}
